package cn.study.controller;


import cn.study.entity.StudyCommodity;
import cn.study.entity.StudyIntegral;
import cn.study.entity.StudyOrder;
import cn.study.service.StudyCommodityService;
import cn.study.service.StudyIntegralService;
import cn.study.service.StudyOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Controller
@Slf4j
@RequestMapping("/study-commodity")
public class StudyCommodityController {
    @Autowired
    StudyCommodityService studyCommodityService;

    @Autowired
    StudyIntegralService studyIntegralService;


    @Autowired
    StudyOrderService studyOrderService;

    @GetMapping("/getcommodity/{id}")
    public String getCommodity(@PathVariable("id") Integer id){



        return "commodityid";
    }

    //商品列表+分页
    @GetMapping("/getcommoditylist")
    public String getCommodity(Model model,
                          @RequestParam(value="pn",defaultValue="1") Integer pn,
                          @RequestParam(value="paginalsize",defaultValue = "3") Integer paginalsize){
        PageHelper.startPage(pn, paginalsize);
        List<StudyCommodity> studyCommodities=studyCommodityService.list();
        PageInfo pInfo=new PageInfo(studyCommodities,10);
        model.addAttribute("pInfo",pInfo);
        return "commodity";
    }



    //立即购买
    @RequestMapping("/gm")
    @ResponseBody
    public String gm(Integer commodityIntegral,Integer id, Integer commodityCont,String commodityName,Integer sid)
    {
        QueryWrapper<StudyIntegral> queryWrapper=new QueryWrapper();
        queryWrapper.eq("integral_emp_id",1001);
        StudyIntegral studyIntegral= studyIntegralService.getOne(queryWrapper);
        int jifen=studyIntegral.getIntegral();
        int jid=studyIntegral.getId();
        System.out.println("asdasdadasdasdasd"+jifen);
        System.out.println("asdasdadasdasdasd"+commodityCont);
        if(commodityIntegral>jifen)
        {
            return "积分不足";
        }
                int commodityCont1=commodityCont-1;
                if(commodityCont1<0)
                {
                    return "库存不足";
                }
                StudyCommodity studyCommodity=new StudyCommodity();
                studyCommodity.setId(sid);
                studyCommodity.setCommodityCont(commodityCont1);
                boolean a= studyCommodityService.updateById(studyCommodity);
                if(!a){
                   return "库存异常";
                }
                int Integral=jifen-commodityIntegral;
                StudyIntegral studyIntegral1=studyIntegralService.getById(jid);
                studyIntegral1.setIntegral(Integral);
                boolean b=studyIntegralService.updateById(studyIntegral1);
                if(!b){
                    return "积分异常";
                }
                StudyOrder studyOrder=new StudyOrder();
                studyOrder.setOrderEmpId(id);
                studyOrder.setOrderCommodityName(commodityName);
                boolean c= studyOrderService.save(studyOrder);
               if(!c){
                   return "订单新增异常";
               }
               return "购买成功";


    }
}

